import requests
import os
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
    'Referer': 'http://www.kuwo.cn/search/list?key=%E5%91%A8%E6%9D%B0%E4%BC%A6',
    'csrf': 'RUJ53PGJ4ZD',
    'Cookie': 'Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1577029678,1577034191,1577034210,1577076651; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1577080777; kw_token=RUJ53PGJ4ZD'
}

headers1 = {
    'Accept': 'application/json, text/plain, */*',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'Connection': 'keep-alive',
    'Cookie': '_ga=GA1.2.1391553170.1626322496; _gid=GA1.2.2113374130.1626322496; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1626322496,1626322611,1626322656; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1626322994; kw_token=W998JL3LCIC',
    'csrf': 'W998JL3LCIC',
    'Host': 'kuwo.cn',
    'Referer': 'http://kuwo.cn/search/list?key=%E8%9C%9C%E9%9B%AA%E5%86%B0%E5%9F%8E',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.67',

}

headers2={'Accept':'application/json, text/plain, */*',
'Accept-Encoding':'gzip, deflate',
'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Connection':'keep-alive',
'Cookie':'_ga=GA1.2.1391553170.1626322496; _gid=GA1.2.2113374130.1626322496; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1626322496,1626322611,1626322656; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1626327995; kw_token=AU8UTFY567C',
'Host':'kuwo.cn',
'Referer':'http://kuwo.cn/search/list?key=%E8%9C%9C%E9%9B%AA%E5%86%B0%E5%9F%8E',
}
musicnum = 30
folder = './music/'
song = input('搜索曲名或歌手：')
page = int(input('搜索页数：'))

url = f'http://kuwo.cn/api/www/search/searchMusicBykeyWord?key={song}&pn={page}&rn=30&httpsStatus=1&reqId=22111e71-e525-11eb-9af1-bf9464ba37c9'
response = requests.get(url, headers=headers1)
json_data = response.json()
rids = [json_data['data']['list'][i]['rid'] for i in range(musicnum)]
artists = [json_data['data']['list'][i]['artist'] for i in range(musicnum)]
names = [json_data['data']['list'][i]['name'] for i in range(musicnum)]
print('找到以下歌曲：')
print('{}\t\t\t\t{}\t\t\t\t{}'.format('序号','曲名','作者'))
for i in range(musicnum):
    print('{}\t\t{}\t\t{}'.format(i,names[i],artists[i]))
num = int(input('输入下载序号：'))
music_url=f'http://kuwo.cn/url?format=mp3&rid={rids[num]}&response=url&type=convert_url3&br=128kmp3&from=web&t=1626328273133&httpsStatus=1&reqId=a99669d1-e530-11eb-9296-45e363740aed'
message_url=f'http://kuwo.cn/api/www/music/musicInfo?mid={rids[num]}&httpsStatus=1&reqId=824b39c0-e529-11eb-82e5-6375643939a5'
twice_response = requests.get(music_url,headers=headers)
target_url = twice_response.json()["url"]
print(target_url)
third_response = requests.get(target_url,headers=headers)
target_music = third_response.content
if not os.path.exists(folder):
    os.mkdir(folder)
with open(folder+names[num]+'.mp3','wb') as f:
    f.write(target_music)
print("Successfully!")